﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace homework10
{
    /// <summary>
    /// WinStudentEdit.xaml 的交互逻辑
    /// </summary>
    public partial class WinStudentEdit : Window
    {
        public WinStudentEdit()
        {
            InitializeComponent();
        }
        public School school { get; set; } = new School();
        public Class xclass{ get; set; } = new Class();
        public Student studentToEdit{ get; set; }= new Student();
        public int action { get; set; } = 0;

        public bool AddStudent(Student student)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Students.Add(student);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Add Student:" + student.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool UpdateStudent(Student student)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Students.Update(student);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Update Student:" + student.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string strName = txtStudentName.Text;
            strName = strName.Trim();

            string strGender = (cbxGender.SelectedItem.ToString()=="男")?"男":"女";
            DateTime dtEDT = (DateTime)dtEnrollDate.SelectedDate;
            if (strName != "")
                switch (action)
                {
                    case 1:
                        AddStudent(new Student { ClassId = xclass.ClassId, Name = strName, Gender=strGender, EnrollDate=dtEDT });
                        break;

                    case 2:
                        studentToEdit.Name = strName;
                        studentToEdit.Gender = strGender;
                        studentToEdit.EnrollDate = dtEDT;
                        UpdateStudent(studentToEdit);
                        break;
                    default: break;
                }
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtSchoolID.Text = school.SchoolId.ToString();
            txtSchool.Text = school.Name;
            txtClassID.Text = xclass.ClassId.ToString();
            txtClass.Text = xclass.Name;
            txtStudentID.IsReadOnly = true;
            cbxGender.Items.Clear();
            cbxGender.Items.Add("男");
            cbxGender.Items.Add("女");
            cbxGender.SelectedIndex = 0;
            dtEnrollDate.SelectedDateFormat = DatePickerFormat.Short;
            dtEnrollDate.SelectedDate=System.DateTime.Now;
            switch (action)
            {
                case 1:
                    lblCaption.Content = "新增学生";
                    txtStudentID.Text = "";
                    break;

                case 2:
                    lblCaption.Content = "修改学生信息";
                    txtStudentID.Text = studentToEdit.StudentId.ToString();
                    txtStudentName.Text = studentToEdit.Name;
                    cbxGender.SelectedIndex = (studentToEdit.Gender == "男") ? 0 : 1;
                    dtEnrollDate.SelectedDate = studentToEdit.EnrollDate;
                    break;

                default:
                    break;

            }
        }
    }
}
